package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.modular.system.model.GameLog;
import cn.stylefeng.guns.modular.system.model.GameUser;
import cn.stylefeng.guns.modular.system.model.Room;
import cn.stylefeng.guns.modular.system.dao.RoomMapper;
import cn.stylefeng.guns.modular.system.service.IGameLogService;
import cn.stylefeng.guns.modular.system.service.IRoomService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.nineshadow.games.Enum.GameTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 房间 服务实现类
 * </p>
 *
 * @author liyong
 * @since 2019-11-28
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {

    @Autowired
    private IGameLogService gameLogService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createRoom(GameTypeEnum gameType, List<GameUser> gameUsers)throws Exception {
        Room room = new Room();
        room.setGameType(gameType.getType());
        baseMapper.insert(room);
        for (GameUser gameUser : gameUsers) {
            GameLog gameLog = new GameLog();
            gameLog.setRoomId(room.getId());
            gameLog.setUserId(gameUser.getId());
            gameLog.setGameType(gameType.getType());
            gameLogService.insert(gameLog);
        }
    }
}
